package com.quasar.hpatient.dialog;

import android.app.Activity;
import android.text.TextUtils;
import android.widget.EditText;

import com.quasar.hpatient.R;

import lib.quasar.base.dialog.BaseDialog;
import lib.quasar.util.InputUtil;
import lib.quasar.widget.menu.NavigationView;

public class MessageDialog extends BaseDialog {

    private OnDialogChangeListener listener;

    public MessageDialog(Activity activity) {
        super(activity);
    }

    @Override
    protected int initView() {
        return R.layout.layout_dialog_message;
    }

    @Override
    protected void initData() {

        final NavigationView menu = findViewById(R.id.dialog_message_menu);
        menu.setOnNavigationChangeListener((isBack, isMenu) -> {
            if (isBack) dismiss();
            else if (isMenu) {
                if (null != listener) {
                    EditText edit = findViewById(R.id.dialog_message_input);
                    final String trim = edit.getText().toString().trim();
                    if (!TextUtils.isEmpty(trim)) {
                        listener.onSucc(trim);
                    }
                }
                cancel();
            }
        });
    }

    @Override
    public void show() {
        super.show();

        final EditText text = findViewById(R.id.dialog_message_input);
        if (null == text) return;
        text.requestFocus();
        InputUtil.openKeybord(text);
    }

    @Override
    public void dismiss() {

        final EditText text = findViewById(R.id.dialog_message_input);
        if (null != text) {
            InputUtil.closeKeybord(text);
        }

        super.dismiss();
    }

    public void setInput(String str) {

        if (TextUtils.isEmpty(str)) return;

        final EditText edit = findViewById(R.id.dialog_message_input);
        if (null == edit) return;

        edit.setText(str);
        edit.setSelection(str.length());
    }

    public void setTitle(String str) {
        if (TextUtils.isEmpty(str)) return;
        NavigationView menu = findViewById(R.id.dialog_message_menu);
        menu.setTitle(str);
    }

    public void setOnDialogChangeListener(OnDialogChangeListener mListener) {
        this.listener = mListener;
    }

    public interface OnDialogChangeListener {
        void onSucc(String message);
    }
}
